﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using DataLayer.Repo;
using DataLayer.Types;
using Microsoft.Win32;
using StructureMap;

namespace DataImporterExporter
{
    /// <summary>
    /// Interaction logic for CheatSheet.xaml
    /// </summary>
    public partial class CheatSheet : Window
    {
        public CheatSheet()
        {
            InitializeComponent();
        }

        #region functions
        private void Browse(TextBox textBox)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            Nullable<bool> result = dialog.ShowDialog();
            if (result == false) return;

            textBox.Text = dialog.FileName;
        }

        private void DoImport<T>(TextBox textField)
        {
            ObjectsToSerialize<T> objectToSerialize = new ObjectsToSerialize<T>();
            Serializer<T> serializer = new Serializer<T>();

            objectToSerialize = serializer.DeSerializeObject(textField.Text);
            ObjectFactory.GetInstance<IRepository<T>>().AddRange(objectToSerialize.ItemList);

            MessageBox.Show("Import was successful");
        }


        private void DoExport<T>(TextBox textField)
        {
            ObjectsToSerialize<T> objectToSerialize = new ObjectsToSerialize<T>();
            Serializer<T> serializer = new Serializer<T>();

            objectToSerialize.ItemList = ObjectFactory.GetInstance<IRepository<T>>().FindAll();
            serializer.SerializeObject(textField.Text, objectToSerialize);

            MessageBox.Show("Export was successful");
        }
        #endregion functions

        private void clickImportFrequency(object sender, RoutedEventArgs e)
        {
            DoImport<FrequencyItem>(txtFrequency);
        }
        private void clickExportFrequency(object sender, RoutedEventArgs e)
        {
            DoExport<FrequencyItem>(txtFrequency);
        }
        private void clickBrowseFrequency(object sender, RoutedEventArgs e)
        {
            Browse(txtFrequency);
        }


        private void clickImportCategory(object sender, RoutedEventArgs e)
        {
            DoImport<CategoryItem>(txtCategory);
        }
        private void clickExportCategory(object sender, RoutedEventArgs e)
        {
            DoExport<CategoryItem>(txtCategory);
        }
        private void clickBrowseCategory(object sender, RoutedEventArgs e)
        {
            Browse(txtCategory);
        }

        private void clickBrowseBalanceType(object sender, RoutedEventArgs e)
        {
            Browse(txtBalanceType);
        }

        private void clickImportBalanceType(object sender, RoutedEventArgs e)
        {
            DoImport<BalanceType>(txtBalanceType);
        }

        private void clickExportBalanceType(object sender, RoutedEventArgs e)
        {
            DoExport<BalanceType>(txtBalanceType);
        }
    }
}
